﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp.Classex
{
    public static class ObjectEx
    {
        public static byte[] ToByteArray(this object _Object)
        {
            using (System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream())
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                            = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                _BinaryFormatter.Serialize(_MemoryStream, _Object);
                return _MemoryStream.ToArray();
            }
        }

        public static object ToObject(this byte[] _byteArrary)
        {
            using (System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_byteArrary))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                                = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                _MemoryStream.Position = 0;
                return _BinaryFormatter.Deserialize(_MemoryStream);
            }
        }
    }
}